package cn.zhy.basic.service.impl;

import cn.zhy.basic.constants.VerifyCodeConstants;
import cn.zhy.basic.dto.SmsCodeDto;
import cn.zhy.basic.exception.BusinessException;
import cn.zhy.basic.service.IVerifyCodeService;
import cn.zhy.basic.util.StrUtils;
import cn.zhy.basic.util.VerifyCodeUtils;
import cn.zhy.user.domain.User;
import cn.zhy.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;
@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;



    @Override
    public String getImgCode(String key) {
        //生成-获取随机验证码
        String code = StrUtils.getComplexRandomString(4);
        //设置到redis
        redisTemplate.opsForValue().set(key, code, 5, TimeUnit.MINUTES);
        //code进行编码base64最后以流的方式
        String base64 = VerifyCodeUtils.verifyCode(115, 40, code);
        return base64;
    }

    @Override
    public void smsCode(SmsCodeDto smsCodeDto) {
        //1.空值校验
        String phone = smsCodeDto.getPhone();
        String ImageCode = smsCodeDto.getImageCode();
        String ImageCodeKey = smsCodeDto.getImageCodeKey();

        if (StringUtils.isEmpty(phone)) {
            throw new BusinessException("手机号不能为空");
        }
        if (StringUtils.isEmpty(ImageCode)) {
            throw new BusinessException("图片验证码不能为空");
        }
        if (StringUtils.isEmpty(ImageCodeKey)) {
            throw new BusinessException("请重新获取图片验证码");
        }

        //检验手机号是否被注册
        User user = userMapper.loadByPhone(phone);
        if (user !=null){
            throw new BusinessException("你已经注册请直接登录");
        }


        //图片验证码校验
        //图片验证码是否过期
        Object objCode = redisTemplate.opsForValue().get(ImageCodeKey);
        if (objCode == null) {
            throw new BusinessException("图片验证码已经过期，请重新输入");
        }

        //验证图片验证码是否正确
        if (!objCode.toString().equalsIgnoreCase(ImageCode)) {
            throw new BusinessException("图片验证码错误");
        }

        //判断手机验证码是否过期
        //先获取手机验证码，如果没有就发送一个验证码  key[register:18708146200] : value[code:time]
        Object objPhoneCode = redisTemplate.opsForValue().get("register:" + phone);
        String code = null;
        if (objPhoneCode == null) {//第一次发送或者过期了
            code = StrUtils.getComplexRandomString(6);
        } else {
            //验证是否过了重发时间
            //以前保存的时间
            long redisTime = Long.parseLong(objPhoneCode.toString().split(":")[1]);
            //获取现在的时间
            long nowTime = System.currentTimeMillis();
            //6.没有过期-判断是过了重发时间
            if ((nowTime - redisTime) < 1 * 60 * 1000) {
                throw new BusinessException("请勿重复获取验证码");
            } else {
                //就用以前的验证码redisSmsCode
                code = objPhoneCode.toString().split(":")[0];
            }
        }
        //将验证码存入redis【register:18515711023 -9527:122335555】
        redisTemplate.opsForValue().set("register:" + phone, code, 3, TimeUnit.MINUTES);
        System.out.println("你的验证码为：" + code + "，请在3分钟之内使用!");


    }
    //=========微信手机绑定================================
    @Override
    public void binderSmsCode(SmsCodeDto smsCodeDto) {
            //获取电话
            String phone = smsCodeDto.getPhone();
            if(StringUtils.isEmpty(phone)){
                throw new BusinessException("手机号不可为空");
            }

            //获取手机验证码判断是否过期   9527:123355555
            Object redisSmsCode = redisTemplate.opsForValue().get(VerifyCodeConstants.BINDER_PREFIX + phone);
            String code =null;
            //第一次发送或者过期了
            if (redisSmsCode ==null){
                //过期了就重新发送一个验证码
                code = StrUtils.getComplexRandomString(6);
            }else {
                //验证是否过来重发时间
                //以前保存的时间
                long redisTime = Long.parseLong(redisSmsCode.toString().split(":")[1]);
                //获取现在的时间
                long nowTime = System.currentTimeMillis();
                //6.没有过期-判断是过了重发时间
                if ((nowTime-redisTime)<1*60*1000){
                    throw new BusinessException("请勿重复获取验证码");
                }else {
                    //就用以前的验证码redisSmsCode
                    code = redisSmsCode.toString().split(":")[0];
                }
            }
            //将验证码存入redis【register:18515711023 -9527:122335555】
            redisTemplate.opsForValue().set(VerifyCodeConstants.REGISTER_CODE_PREFIX+phone, code, 3, TimeUnit.MINUTES);
            //发短信
            System.out.println("你的验证码为：" + code + "，请在3分钟之内使用!");

        }

    }


